import Vue from 'vue'
import axios from 'axios'

import 'element-ui/lib/theme-chalk/index.css';
import './assets/common.css'

import './assets/beautify';
import './assets/beautify-css';
import './assets/beautify-html';

import App from './App'
import router from './router'
import store from './store'

import _DB from './localDB';
import moment from 'moment';

import ElementUI from 'element-ui';
import { Message } from 'element-ui';
import { Loading } from 'element-ui';

import mavonEditor from 'mavon-editor'
import 'mavon-editor/dist/css/index.css'
// use
Vue.use(mavonEditor)

Vue.use(ElementUI);
Vue.config.productionTip = false
Vue.prototype.$moment = moment;

window.DB = new _DB();
window.store = store;

const win = require('electron').remote.getCurrentWindow();
const { dialog } = require('electron').remote;//修改默认对话框，修复electron弹出默认对话框后页面失去焦点的bug
Vue.prototype.$alert = function (str) {
  var options = {
    title: 'Requester',
    type: 'warning',
    buttons: ["确定"],
    defaultId: 0,
    cancelId: 0,
    detail: str,
    message: ''
  }
  dialog.showMessageBoxSync(win, options)
}
Vue.prototype.$confirm = function (str) {
  var options = {
    title: 'Requester',
    type: 'warning',
    buttons: ["确认", "取消"],
    defaultId: 0,
    cancelId: 1,
    detail: '',
    message: str
  }
  var flag = dialog.showMessageBoxSync(win, options);
  if (flag == 0) {
    return true;
  } else {
    return false;
  }
}

Vue.prototype.$win = win;

if (!process.env.IS_WEB) Vue.use(require('vue-electron'))
Vue.http = Vue.prototype.$http = axios;


window.$http = function (options) {
  if (options.loading !== false) {
    var loading = Loading.service({
      lock: true,
    });
  };
  return axios({
    method: options.type,
    url: options.url,
    data: options.data ? options.data : undefined,
    params: options.params ? options.params : undefined,
    headers: options.headers ? options.headers : undefined,
  }).then((res) => {
    if (options.loading !== false) {
      loading.close();
    }
    options.success(res.data);
  }).catch((error) => {
    if (options.loading !== false) {
      loading.close();
    }
    Message({
      message: error.response && error.response.data && error.response.data.msg ? error.response.data.msg : error,
      type: 'error'
    }, true);
    if (options.error) {
      options.error(error);
    };
  })
};

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  components: { App },
  router,
  store,
  template: '<App/>'
}).$mount('#app')

document.addEventListener('keydown', function (e) {
  e = window.event || e;
  var keycode = e.keyCode || e.which;
  if (e.ctrlKey && keycode == 187) {
    e.preventDefault();
    window.event.returnValue = false;
  };
  if (e.ctrlKey && keycode == 189) {
    e.preventDefault();
    window.event.returnValue = false;
  };
  if (e.ctrlKey && keycode == 107) {
    e.preventDefault();
    window.event.returnValue = false;
  };
  if (e.ctrlKey && keycode == 109) {
    e.preventDefault();
    window.event.returnValue = false;
  };
});